import threading

# 创建ThreadLocal对象
thread_local_data = threading.local()


# 定义一个函数，用于设置线程本地变量
def set_local_data(value):
    thread_local_data.value = value


# 定义一个函数，用于获取线程本地变量
def get_local_data():
    return getattr(thread_local_data, "value", None)


# 创建线程函数
def thread_function():
    set_local_data("Hello from thread")
    print(f"Thread local data: {get_local_data()}")


# 创建两个线程
thread1 = threading.Thread(target=thread_function)
thread2 = threading.Thread(target=thread_function)

# 启动线程
thread1.start()
thread2.start()

# 等待线程执行完成
thread1.join()
thread2.join()
